# Services/api/data_generator_api.py
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel
from Services.data_generator import PurchaseExchangeDataGenerator


# 定义请求模型
class GenerateRequest(BaseModel):
    num_records: int


# 创建路由实例
router = APIRouter(
    prefix="/api/data-generator",
    tags=["data-generator"]
)

# 初始化数据生成器实例
data_generator = PurchaseExchangeDataGenerator()


@router.post("/generate-batch")
async def generate_batch_records(request: GenerateRequest):
    """
    生成指定数量的购汇批次记录
    """
    if request.num_records <= 0 or request.num_records > 10000:
        raise HTTPException(status_code=400, detail="num_records must be between 1 and 10000")

    try:
        records = data_generator.generate_batch_records(request.num_records)
        return {
            "success": True,
            "data": records,
            "count": len(records)
        }
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error generating records: {str(e)}")


@router.get("/generate-single")
async def generate_single_record():
    """
    生成单条购汇批次记录
    """
    try:
        record = data_generator.generate_single_record()
        return {
            "success": True,
            "data": record
        }
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error generating record: {str(e)}")
